<?php
/**
 * Wrapper for wget command
 * Can keep cookies between requests
 * Constants:
 * WGET_WRAPPER_TEMPDIR: Temp dir, default: sys_get_temp_dir();
 * WGET_WRAPPER_EXE: exe of wget (default linux: wget, default windows: wget.exe)
 *
 * TODO: Do not output command output
 * @author Naxan
 */
class WGet_Wrapper {
	
	private $file_temp;
	private $file_cookies = "";
	private $_filesTemp = array();
	
	static $EXE = "wget";
	static $TEMP_DIR;
	
	/**
	 * Get the value of an option, with a default value
	 */
	public static function getOptionValue($options, $name, $default = null) {
		$value = $default;
		if (isset($options[$name])) {
			$value = $options[$name];
		}
		return $value;
	}
	
	/**
	 * Send a new request
	 */
	public function send($url,$post = array(),$options = array())
	{
		$strdata = "";
		
		$save_cookies = self::getOptionValue($options, "saveCookies", true);
		$referer = self::getOptionValue($options, "referer", "");
		$useragent = self::getOptionValue($options, "useragent", "");
		
		
		if (is_array($post)) {
			foreach ($post as $key => $val)
			{
				if ($strdata != "") {
					$strdata .= "&";
				}
				$strdata .= "$key=".urlencode($val);
			}
		}
		$remote = "local";
		if (isset($_SERVER['REMOTE_ADDR'])) {
			$remote = $_SERVER['REMOTE_ADDR'];
		}
		$this->file_temp = $this->getFileTemp($remote);
		
		$arguments = '--output-document="'.$this->file_temp.'" --post-data="'.$strdata.'"';
		if ($referer != "") {
			$arguments .= ' --header="Referer: '.$referer.'"';
		}
		if (!empty($useragent)) {
			$arguments .= ' --user-agent="'.$useragent.'"';
		}
		if ($this->file_cookies != "") {
			$arguments .= ' --load-cookies "'.$this->file_cookies.'"';
		}
		if ($save_cookies) {
			if (empty($this->file_cookies)) {
				$this->file_cookies = $this->getFileTemp($remote,"cookies_");
			}
			$arguments .= ' --cookies=on --keep-session-cookies --save-cookies "'.$this->file_cookies.'"';
		}
		$exe = self::$EXE;
		$nul = "/dev/null";
		if (PHP_OS == "WINNT") {
			$nul = 'NUL';
		}
		$command = $exe.' '.$arguments.' "'.$url.'" 2> '.$nul;
		exec($command,$output,$return);
		$rep = file_get_contents($this->file_temp);

		// unlink($this->file_temp);
		return $rep;
	}
	
	/**
	 * Shutdown the command
	 * Must be used at the end of the script, to clear temp files
	 */
	public function close()
	{
		foreach ($this->_filesTemp as $f)
		{
			if (file_exists($f))
			{
				//unlink($f);
			}
		}
	}
	
	/**
	 * Get a name of temporary file
	 */
	private function getFileTemp($ip, $subname = "")
	{
		$folder = self::$TEMP_DIR;
		$i = 0;
		while (file_exists($folder."WGet_$subname".$ip.".".$i)) {
			$i++;
		}
			
		array_push($this->_filesTemp,$folder."WGet_$subname".$ip.".".$i);
		return $folder."WGet_$subname".$ip.".".$i;
	}
	
	/**
	 * Convertit en UTF8
	 */
	public static function toUTF8(&$str)
	{
		if (is_array($str))
		{
			foreach ($str as &$v)
			{
				if (mb_detect_encoding($v."e",'UTF-8, ISO-8859-1') != "UTF-8") {
					$v = utf8_encode($v);
				}
			}
		}
		else
		{
			if (mb_detect_encoding($str."e",'UTF-8, ISO-8859-1') != "UTF-8") {
				$str = utf8_encode($str);
			}
		}
	}
	
	
	/**
	 * Convert into current charset
	 */
	public static function toCurrentCharset(&$str)
	{
		self::toUTF8($str);
		if (is_array($str))
		{
			foreach ($str as &$v)
			{
				if (mb_detect_encoding($v."e",'UTF-8, ISO-8859-1') != "UTF-8")
					$v = utf8_decode($v);
			}
		}
		else
		{
			if (mb_detect_encoding($str."e",'UTF-8, ISO-8859-1') != "UTF-8")
				$str = utf8_decode($str);
		}		
	}
}

/**
 * Conf files
 */
if (defined('WGET_WRAPPER_TEMPDIR')) {
	WGet_Wrapper::$TEMP_DIR = WGET_WRAPPER_TEMPDIR;	
}
else {
	WGet_Wrapper::$TEMP_DIR = sys_get_temp_dir().DIRECTORY_SEPARATOR;
}

if (defined('WGET_WRAPPER_EXE')) {
	WGet_Wrapper::$EXE = WGET_WRAPPER_EXE;
}

?>
